package com.huadi.ssm.controller;



import com.huadi.ssm.page.Page;
import com.huadi.ssm.po.Admin;
import com.huadi.ssm.service.AdminService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * 管理员管理
 *
 */
@RequestMapping("/admin")
@Controller
public class AdminController {

    @Autowired
    public AdminService adminService;

    /**
     * 管理员列表页
     * @param model
     * @return
     */
    @RequestMapping(value="/list",method= RequestMethod.GET)
    public ModelAndView list(ModelAndView model){
        model.setViewName("admin/admin_list");
        return model;
    }

    /**
     * 获取管理员列表
     * @param name
     * @param page
     * @return
     */
    @RequestMapping(value="/get_list",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getList(
            @RequestParam(value="name",required=false,defaultValue="") String name,
            Page page
    ){
        Map<String, Object> ret = new HashMap<String, Object>();
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("name", "%"+name+"%");
        queryMap.put("offset", page.getOffset());
        queryMap.put("pageSize", page.getRows());
        ret.put("rows", adminService.findList(queryMap));
        ret.put("total", adminService.getTotal(queryMap));
        return ret;
    }

    /**
     * 删除管理员操作
     * @param ids
     * @return
     */
    @RequestMapping(value="/delete",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String> delete(
            @RequestParam(value="ids[]",required=true) Long[] ids
    ){
        Map<String, String> ret = new HashMap<String, String>();
        if(ids == null){
            ret.put("type", "error");
            ret.put("msg", "请选择要删除的数据!");
            return ret;
        }
        String idsString = "";
        for(Long id:ids){
            idsString += id + ",";
        }
        idsString = idsString.substring(0,idsString.length()-1);
        if(adminService.delete(idsString) <= 0){
            ret.put("type", "error");
            ret.put("msg", "删除失败!");
            return ret;
        }
        ret.put("type", "success");
        ret.put("msg", "删除成功!");
        return ret;
    }

    /**
     * 编辑管理员操作
     * @param admin
     * @return
     */
    @RequestMapping(value="/edit",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String> edit(Admin admin){
        Map<String, String> ret = new HashMap<String, String>();
        if(admin == null){
            ret.put("type", "error");
            ret.put("msg", "数据绑定出错，请联系开发作者!");
            return ret;
        }
        if(StringUtils.isEmpty(admin.getName())){
            ret.put("type", "error");
            ret.put("msg", "用户名不能为空!");
            return ret;
        }
        if(StringUtils.isEmpty(admin.getPassword())){
            ret.put("type", "error");
            ret.put("msg", "密码不能为空!");
            return ret;
        }
        Admin existUser = adminService.findByName(admin.getName());
        if(existUser != null){
            if(admin.getId() != existUser.getId()){
                ret.put("type", "error");
                ret.put("msg", "该用户名已经存在!");
                return ret;
            }

        }
        if(adminService.edit(admin) <= 0){
            ret.put("type", "error");
            ret.put("msg", "修改失败!");
            return ret;
        }
        ret.put("type", "success");
        ret.put("msg", "修改成功!");
        return ret;
    }


    /**
     * 添加管理员操作
     * @param admin
     * @return
     */
    @RequestMapping(value="/add",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String> add(Admin admin){
        Map<String, String> ret = new HashMap<String, String>();
        if(admin == null){
            ret.put("type", "error");
            ret.put("msg", "数据绑定出错，请联系开发作者!");
            return ret;
        }
        if(StringUtils.isEmpty(admin.getName())){
            ret.put("type", "error");
            ret.put("msg", "用户名不能为空!");
            return ret;
        }
        if(StringUtils.isEmpty(admin.getPassword())){
            ret.put("type", "error");
            ret.put("msg", "密码不能为空!");
            return ret;
        }
        Admin existUser = adminService.findByName(admin.getName());
        if(existUser != null){
            ret.put("type", "error");
            ret.put("msg", "该用户名已经存在!");
            return ret;
        }
        if(adminService.add(admin) <= 0){
            ret.put("type", "error");
            ret.put("msg", "添加失败!");
            return ret;
        }
        ret.put("type", "success");
        ret.put("msg", "添加成功!");
        return ret;
    }
}
